﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoboRace_Shared;

namespace RoboRace_Server
{
    //Public event delegate declarations.
    public delegate void PlayerJoin(Player player);
    public delegate void PlayerNoJoin(Player player);
    public delegate void GameStarted(string message);
    public delegate void GameStopped(string message);
    public delegate void GameOver(string message);
    public delegate void PlayerRemoved(Guid guid);
    public delegate void RobotsUpdated(List<Robot> robots);

    [Serializable]
    public class Game
    {
        public event PlayerJoin OnPlayerJoin;
        public event PlayerNoJoin OnPlayerNoJoin;
        public event GameStarted OnGameStarted;
        public event GameStopped OnGameStopped;
        public event GameOver OnGameOver;
        public event PlayerRemoved OnPlayerRemoved;
        public event RobotsUpdated OnRobotsUpdated;

        private int _playerCount;
        private Board _gameBoard;
        private List<Player> _players;
        private Boolean _isStarted;
        private List<Robot> _robots;

        public int PlayerCount { get { return _playerCount; } }
        public Boolean IsRunning { get { return _isStarted; } }
        public Board Board { get { return _gameBoard; } }
        public List<Robot> Robots { get { return _robots; } }

        public Game(int numPlayers)
        {
            this._playerCount = numPlayers;
            this._gameBoard = new Board(5, 5);
            this._players = new List<Player>();
            this._isStarted = false;
            this._robots = new List<Robot>();
        }

        private void Start()
        {
            if (this._isStarted != true)
            {
                OnRobotsUpdated(this._robots);
                this._isStarted = true;
                OnGameStarted("Game has started!");
            }
        }

        private void Stop()
        {
            if (this._isStarted != false)
            {
                this._isStarted = false;
                OnGameStopped("Game has ended.");
            }
        }

        public void Join(Player player)
        {
            if (this._isStarted != true)
            {
                this._players.Add(player);
                this._robots.Add(new Robot(player.Name, player.Guid));
                OnPlayerJoin(player);

                if (this._players.Count == _playerCount)
                {
                    Start();
                }
            }
            else
            {
                OnPlayerNoJoin(player);
            }
            
        }

        public void Disconnect(Guid guid)
        {
            foreach (Player p in this._players)
            {
                if (p.Guid == guid)
                {
                    this._players.RemoveAt(this._players.IndexOf(p));
                    
                    foreach (Robot r in this._robots)
                    {
                        if (r.PlayerGuid == guid)
                        {
                            this._robots.RemoveAt(this._robots.IndexOf(r));
                            OnRobotsUpdated(this._robots);
                            break;
                        }
                    }
                    OnPlayerRemoved(guid);
                    break;
                }
            }

            if (this._players.Count < _playerCount)
            {
                Stop();
            }
        }

        private void ShuffleProgramCards()
        {

        }

        private void DealProgramCards()
        {

        }

    }
}
